require 'albacore'

def get_svn_rev()
    info = `svnversion`
    rev = $1 if info =~ /(?:\d+\:)*(\d+)/
end

def make_version(assembly_info_file, build_number)
    base_version = '0.0.0'
    text = File.read(assembly_info_file)
    base_version = $1 if text =~ /AssemblyVersion\s*\("(\d+\.\d+\.\d+)/
    version = base_version + '.' + build_number
end

$project_name = 'NRex'
$solution_file = 'NRex.sln'
$assembly_info_file = 'src/CommonAssemblyInfo.cs'
$staging_area_dir = 'staging_area'

$configuration = ENV['config'] || 'Release'
$platform = ENV['platform'] || 'Any CPU'
case $platform
when 'Any CPU'
    $build_output_dir = File.join('bin', $configuration)
when 'x86', 'x64'
    $build_output_dir = File.join('bin', $platform, $configuration)
end

build_number = get_svn_rev() || '0'
$version = make_version($assembly_info_file, build_number)

puts
puts "Building #{$solution_file}..."
puts "Configuration: #{$configuration}"
puts "Platform: #{$platform}"
puts "Version: #{$version}"
puts

################################################################################

# Albacore configuration task run before any other task: configure default Albacore task properties
Albacore.configure do |config|
    config.log_level = :verbose
    config.nunit.command = $platform == 'x86' ? 'tools/nunit/nunit-console-x86.exe' : 'tools/nunit/nunit-console.exe'
    config.msbuild.solution = $solution_file
end

################################################################################

task :default
task :all => [:versioning, :rebuild, :artifacts]

################################################################################

desc 'Run a build of the whole solution'
msbuild :build do |t|
    t.properties :configuration => $configuration, :Platform => $platform
    t.targets :Build
end

desc 'Clean the whole solution'
msbuild :clean do |t|
    t.properties :configuration => $configuration, :Platform => $platform
    t.targets :Clean
end

desc 'Run a clean build of the whole solution'
task :rebuild => [:clean, :build]

desc 'Update assembly info file'
assemblyinfo :versioning do |t|
    t.input_file = $assembly_info_file
    t.version = $version
    t.file_version = $version
    t.output_file = $assembly_info_file
end

################################################################################

desc 'Create artifacts for current build configuration'
task :artifacts => [:prepare_staging_area,
                    :zip_artifacts,
                    :delete_staging_area]

task :prepare_staging_area do
    FileUtils.remove_dir($staging_area_dir) if File.directory? $staging_area_dir
    FileUtils.mkdir $staging_area_dir
    
    FileList[File.join($build_output_dir, '*.exe')].exclude(/vshost\.exe/).each do |file|
        FileUtils.cp file, $staging_area_dir, :verbose => true
    end
end

task :delete_staging_area do
    FileUtils.remove_dir($staging_area_dir) if File.directory? $staging_area_dir
end

zip :zip_artifacts do |zip|
    zip.directories_to_zip $staging_area_dir
    zip.output_file = $project_name
    zip.output_file += '-' + $platform.gsub(/ /, '') unless $platform == 'Any CPU'
    zip.output_file += '-' + $version + '.zip'
    zip.output_path = '.'
    # puts "generating #{zip.output_file}..."
end
